<!--
 * @Description: 以太口配置
 * @Author: 0001101008/HuYuxi
 * @Date: 2021-09-11 16:07:31
 * @LastEditTime: 2021-09-28 10:28:08
 * @LastEditors: 0001101008/HuYuxi
-->
<template>
  <div class="page-content" style="padding: 0">
    <div class="table-top">
      <!-- 刷新 -->
      <el-button size="medium" plain @click="reset">刷新</el-button>
      <!-- 新建 -->
      <el-button size="medium" type="primary" plain @click="handleCreate" v-power="2">新建</el-button>
    </div>
    <tao-table
      class="table-box"
      :data="dataList"
      :page="page"
      @changePage="changePage"
      :showPage="true"
      ref="table"
    >
      <el-table-column label="ID" align="center" width="100" sortable prop="id" />
      <el-table-column label="端口" min-width="200" prop="intfName">
        <template slot-scope="scope">
          <span>{{scope.row.intfName.toUpperCase()}}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作模式" align="center" width="150" prop="workMode" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.workMode === 'AUTO_NEGOTIATION'">自协商</span>
          <span v-if="scope.row.workMode === 'HALF_100M'">半双工100M</span>
          <span v-if="scope.row.workMode === 'FULL_100M'">全双工100M</span>
          <span v-if="scope.row.workMode === 'FULL_1G'">全双工1G</span>
        </template>
      </el-table-column>
      <el-table-column label="管理状态" width="130" align="center" prop="adminStatus" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.adminStatus">
            <i class="i-zy"></i>
            UP
          </span>
          <span v-else>
            <i class="i-yz"></i>
            DOWN
          </span>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column fixed="right" align="center" label="操作" width="160px">
        <!--  删除-->
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="showDialog('edit', scope)"
            v-power="ne.dgId === 100000 ? 0 : 2"
          >编辑</el-button>
          <el-button
            type="text"
            style="color: #FA6962"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            v-power="ne.dgId === 100000 ? 0 : 2"
          >删除</el-button>
        </template>
      </el-table-column>
    </tao-table>
    <forms
      v-if="dialogStatus"
      :ne="ne"
      :type="type"
      :dialogStatus="dialogStatus"
      :ethernetInfo="ethernetInfo"
      :portList="portList"
      @handleDialog="handleDialog($event)"
    />
  </div>
</template>

<script>
import forms from './forms';
export default {
  props: ['ne'],
  components: {
    forms
  },
  data () {
    return {
      dataList: [],
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      },
      type: 'create',
      dialogStatus: false,
      ethernetInfo: {},
      portList: [],
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.refresh();
      _this.getProtList();
    });
  },
  methods: {
    // 获取端口
    getProtList () {
      let _this = this;
      _this.$http.get({
        url: '/api/sdwan/ethport/getStaticsPorts/' + _this.ne.id,
      }).then((res) => {
        let { status, resultBody } = res;
        if (status === '0') {
          if (resultBody.length > 0) {
            let list = [];
            resultBody.map(item => {
              if (item.name.indexOf('gei') > -1 || item.name.indexOf('xgei') > -1) {
                if (item.name.indexOf('.') === -1) {
                  list.push(item);
                }
              }
            });
            _this.portList = list;
          }
        }
      });
    },
    // 返回端口状态
    returnPortStatus (port, portList) {
      if (portList.length > 0) {
        for (let i = 0; i < portList.length; i++) {
          if (portList[i].name === port) {
            if (!portList[i].status) {
              return 'UP';
            } else {
              return 'DOWN';
            }
          }
        }
      } else {
        return 'DOWN';
      }
    },

    // 重置
    reset () {
      let _this = this;
      _this.page.page = 1;
      _this.refresh();
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        neId: _this.ne.id,
        limit: _this.page.pageSize,
        offset: (_this.page.page - 1) * _this.page.pageSize
      };
      _this.$http.post({
        url: '/api/sdwan/ethport/query',
        data: params
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          _this.page.total = resultBody.count;
          _this.dataList = resultBody.objects;
        }
      });
    },
    // 过滤显示字段
    changeColumn (columns) {
      this.columns = columns;
      this.$refs.table.doLayout();
    },
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    },
    // 新建
    handleCreate () {
      let _this = this;
      _this.type = 'create';
      _this.dialogStatus = true;
    },
    // 回调
    handleDialog (val) {
      let _this = this;
      _this.dialogStatus = val;
    },
    // 编辑
    showDialog (type, scope) {
      let _this = this;
      _this.type = 'edit';
      _this.ethernetInfo = scope.row;
      _this.dialogStatus = true;
    },
    // 删除
    handleDelete (scope) {
      let _this = this;
      this.$confirm('您确定要删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        _this.$http.deleted({
          url: '/api/sdwan/ethport/' + scope.row.id,
        }).then(res => {
          let { status } = res;
          if (status === '0') {
            _this.$message.success('删除成功！');
            _this.refresh();
            _this.getProtList();
          }
        });
      }).catch(() => {
        _this.$message.info('取消删除');
      });
    }
  }
};
</script>

<style lang='scss' scoped>
.page-content {
  width: 100%;
  height: 100%;
  .table-top {
    margin-bottom: 15px;
  }
  .table-box {
    ::v-deep .el-button {
      padding: 0;
    }
    ::v-deep .el-dropdown-link {
      cursor: pointer;
      color: #409eff;
    }
  }
  .i-yz {
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 8px;
    border-radius: 50%;
    background: #de4040;
    position: relative;
    top: 1px;
  }
  .i-zy {
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 8px;
    border-radius: 50%;
    background: #96da80;
    position: relative;
    top: 1px;
  }
}
</style>